package com.zl.tby.service.impl;

import com.zl.tby.core.base.bean.TbyBaseQuery;
import com.zl.tby.core.base.bean.TbyPageResult;
import com.zl.tby.core.base.enums.TbyEducationViewCategoryEnum;
import com.zl.tby.core.base.util.ToolsUtil;
import com.zl.tby.core.common.R;
import com.zl.tby.model.TbyEducationRoom;
import com.zl.tby.repository.TbyEducationRoomRepo;
import com.zl.tby.service.ITbyEducationRoomService;
import com.zl.tby.vo.TbyEducationRoomForm;
import org.noear.solon.annotation.Component;
import org.noear.solon.data.annotation.Tran;
import org.noear.wood.DbContext;
import org.noear.wood.DbTableQuery;
import org.noear.wood.IPage;
import org.noear.wood.annotation.Db;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 科研宣教-宣教馆
 * @author changpeng
 * @version 1.0
 * @since 2024/8/6 20:22
 */
@Component
public class TbyEducationRoomServiceImpl implements ITbyEducationRoomService {

    @Db
    private TbyEducationRoomRepo tbyEducationRoomRepo;
    @Db
    private DbContext db;

    @Override
    @Tran
    public R<TbyEducationRoom> add(TbyEducationRoomForm form) {
        TbyEducationRoom entity = form.createEntity();
        entity.setCreateBy(ToolsUtil.getLoginUserId());
        entity.setCreateTime(LocalDateTime.now());
        long id = tbyEducationRoomRepo.insert(entity, true);
        entity.setId(id);
        return R.ok(entity);
    }

    @Override
    @Tran
    public R<TbyEducationRoom> update(TbyEducationRoomForm form) {
        TbyEducationRoom entity = this.get(form.getId());
        if (entity == null) {
            return R.failed("数据不存在");
        }
        form.updateEntity(entity);
        entity.setUpdateBy(ToolsUtil.getLoginUserId());
        entity.setUpdateTime(LocalDateTime.now());
        tbyEducationRoomRepo.updateById(entity, false);
        return R.ok(entity);
    }

    @Override
    public R<Void> delete(Long id) {
        // TODO 验证是否被引用
        int flag = tbyEducationRoomRepo.deleteById(id);
        return flag > 0 ? R.ok() : R.failed("删除失败");
    }

    @Override
    public TbyEducationRoom get(Long id) {
        return tbyEducationRoomRepo.selectById(id);
    }

    @Override
    public TbyPageResult<TbyEducationRoom> page(TbyBaseQuery query) {
        IPage<TbyEducationRoom> iPage = tbyEducationRoomRepo.selectPage(query.getOffset(), query.getSize(), mp -> {});
        return new TbyPageResult(iPage, query);
    }

    @Override
    public List<TbyEducationRoom> list() {
        return tbyEducationRoomRepo.selectList(mapperWhereQ -> {});
    }

    @Override
    public void updateImageAndVideoCount() {
        DbTableQuery query = db.table("tby_education_view").whereEq("category", TbyEducationViewCategoryEnum.XJG.toString());
        query.groupBy("business_id");
        try {
            List<TbyEducationRoom> dataList = query.selectList("business_id id, sum(case when type = 'IMAGE' then 1 else 0 end) image_count, sum(case when type = 'VIDEO' then 1 else 0 end) video_count", TbyEducationRoom.class);
            for (TbyEducationRoom room : dataList) {
                if (room.getId() != null)  {
                    tbyEducationRoomRepo.updateById(room, false);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
